﻿/*********************************************************************************
**                                                                              **
**  Copyright (C) 2024 LiLong                                                   **
**  This file is part of GnssLogs.                                              **
**                                                                              **
**  GnssLogs is free software: you can redistribute it and/or modify            **
**  it under the terms of the GNU Lesser General Public License as published by **
**  the Free Software Foundation, either version 3 of the License, or           **
**  (at your option) any later version.                                         **
**                                                                              **
**  GnssLogs is distributed in the hope that it will be useful,                 **
**  but WITHOUT ANY WARRANTY; without even the implied warranty of              **
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               **
**  GNU Lesser General Public License for more details.                         **
**                                                                              **
**  You should have received a copy of the GNU Lesser General Public License    **
**  along with GnssLogs.  If not, see <https://www.gnu.org/licenses/>.          **
**********************************************************************************/
#pragma once

#include "../GRS.h"
#include "Commons.h"

namespace GnssLogs
{
namespace GRSDecoder
{
using namespace tao::pegtl;
using LogHeader = NMEA_TALKER_IDENTIFIER("GRS");
using Mode      = one<'0', '1'>;
// clang-format off
struct Res : Floating {};
// clang-format on
using Expression =
    seq<LogHeader, Split, Utc, Split, Mode, rep<12, Split, opt<Res>>, opt<Split, SystemId, Split, SignalId>, one<'*'>, CheckSum, Tail>;

template<typename T>
struct Action
{
};

template<>
struct Action<LogHeader>
{
    template<typename ParseInput>
    static bool apply(ParseInput& in, GRS::Data& data)
    {
        data.header = std::string(in.begin(), in.size());
        return true;
    }
};

template<>
struct Action<Utc> : UTCHelperNoSetValue
{
};

template<>
struct Action<Mode> : IntHelper<Action<Mode>, uint8_t>
{
    static void setValue(GRS::Data& data, const uint8_t& value) { data.mode = value; }
};

template<>
struct Action<Res> : DoubleHelper<Action<Res>>
{
    static void setValue(GRS::Data& data, double value) { data.res.push_back(value); }
};

template<>
struct Action<SystemId> : SystemIdHelper<Action<SystemId>>
{
    static void setValue(GRS::Data& data, uint8_t value) { data.systemId = value; }
};

template<>
struct Action<SignalId> : SystemIdHelper<Action<SignalId>>
{
    static void setValue(GRS::Data& data, uint8_t value) { data.signalId = value; }
};

template<>
struct Action<CheckSum> : CheckSumHelper<Action<CheckSum>>
{
    static void setValue(GRS::Data& data, uint8_t value) { data.checkSum = value; }
};
} // namespace GRSDecoder
} // namespace GnssLogs